//
//  MAPYCommon.swift
//  Mars Proxy
//
//  Created by PowerfullClean on 2023/10/16.
//

import UIKit
import SnapKit


let mapy_SCREEN_WIDTH  =  UIScreen.main.bounds.size.width
let mapy_SCREEN_HEIGHT =  UIScreen.main.bounds.size.height
class MAPYCommon: NSObject {
    @objc static func mapy_getHXColorValue(rgb: UInt,a:CGFloat = 1) -> UIColor {
        return UIColor(
            red: CGFloat((rgb & 0xFF0000) >> 16) / 255.0,
            green: CGFloat((rgb & 0x00FF00) >> 8) / 255.0,
            blue: CGFloat(rgb & 0x0000FF) / 255.0,
            alpha: a
        )
    }
    
    @objc static var mapy_statusBarHeight: CGFloat {
        return mapy_isVirtualTop ? 44 : 20
    }
    @objc static var mapy_isVirtualTop: Bool {
        if #available(iOS 11.0, *) {
            let keyWindow: UIWindow? = MAPYAppDelegate.mapy_shared().mapy_window ?? nil
            if keyWindow != nil {
                let topSafeInset: CGFloat = keyWindow!.safeAreaInsets.top
                if topSafeInset > 20.0 {
                    return true
                }
            }
        }
        return false
    }
    
    @objc static var mapy_XBottomSpace: CGFloat {
        return mapy_isVirtualHome ? 34 : 0
    }
    
    @objc static var mapy_tabbarH: CGFloat {
        return mapy_XBottomSpace+49
    }
    @objc static var mapy_isPhone6: Bool {
        return mapy_SCREEN_HEIGHT == 667
    }
    @objc static var mapy_isVirtualHome: Bool {
        if #available(iOS 11.0, *) {
            let keyWindow: UIWindow? = MAPYAppDelegate.mapy_shared().mapy_window ?? nil
            if keyWindow != nil {
                let bottomSafeInset: CGFloat = keyWindow!.safeAreaInsets.bottom
                if bottomSafeInset > 0.0 {
                    return true
                }
            }
        }
        return false
    }
    @objc static var mapy_version: String {
        return Bundle.main.infoDictionary!["CFBundleShortVersionString"] as? String ?? ""
    }
    @objc static var mapy_bundleIdentifier: String {
        return Bundle.main.bundleIdentifier ?? ""
        
    }
    @objc static var mapy_buildVersion: String {
        return Bundle.main.infoDictionary!["CFBundleVersion"] as? String ?? ""
        
    }
    @objc static var mapy_idfv: String {
        return UIDevice.current.identifierForVendor?.uuidString ?? ""
    }
    @objc static func mapy_getTopViewController(vc: UIViewController?) -> UIViewController? {
        if let nav = vc as? UINavigationController {
            return mapy_getTopViewController(vc: nav.visibleViewController)
        }
        else if let tab = vc as? UITabBarController, let selected = tab.selectedViewController {
            return mapy_getTopViewController(vc: selected)
        }
        else if let persented = vc?.presentedViewController {
            return mapy_getTopViewController(vc: persented)
        }
        return vc
    }
    static func mapy_alertShow(mapy_title: String, mapy_message: String, mapy_vc: UIViewController? = nil, mapy_comlete:(()->())? = nil) {
        var mapy_vc = mapy_vc
        if mapy_vc == nil {
            mapy_vc = MAPYAppDelegate.mapy_shared().mapy_window?.rootViewController
        }
        DispatchQueue.main.asyncAfter(deadline: .now()+0.2) {
            if mapy_isAlert {
                return
            }
            mapy_isAlert = true
            let alert = UIAlertController(title: mapy_title, message: mapy_message, preferredStyle: .alert)
            let action1 = UIAlertAction.init(title: "Cancel", style: .destructive) { _ in
                mapy_isAlert = false
            }
            let action2 = UIAlertAction.init(title: "Confirm", style: .default) { _ in
                mapy_isAlert = false
                if (mapy_comlete != nil) {
                    mapy_comlete?()
                }else {
                    UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)
                }
            }
            alert.addAction(action1)
            alert.addAction(action2)
            mapy_vc?.present(alert, animated: true)
        }
        
    }
    static var mapy_isAlert: Bool = false
    @objc static let mapy_navHegiht: CGFloat = mapy_statusBarHeight+44+16
    //MARK: mapy value change
    static func mapy_changeSizeValue(size: Double)-> String {
        var f = ""
        var type = "KB/s"
        if (size == 0) {
            f = "0"
            type = "KB/s"
        }
        else if (size >= 1024 && size < pow(1024, 2)) {
            f = String(format: "%.0f", size/1024.00)
            type = "KB/s"
        }else if (size >= pow(1024, 2) && size < pow(1024, 3)) {
            f = String(format: "%.0f", size/pow(1024, 2))
            type = "MB/s"
        }else
        if (size >= pow(1024, 3)) {
            f = String(format: "%.0f", size/pow(1024, 3))
            type = "GB/s"
        }
        return f+type
    }
    
    static func mapy_getSizeValue(size: CGFloat)-> (String, String) {
        var f = ""
        var type = "KB/s"
        if (size == 0) {
            f = "0"
            type = "KB/s"
        }
        else if (size >= 1024 && size < pow(1024, 2)) {
            f = String(format: "%.0f", size/1024.00)
            type = "KB/s"
        }else if (size >= pow(1024, 2) && size < pow(1024, 3)) {
            f = String(format: "%.0f", size/pow(1024, 2))
            type = "MB/s"
        }else
        if (size >= pow(1024, 3)) {
            f = String(format: "%.0f", size/pow(1024, 3))
            type = "GB/s"
        }
        return (f, type)
    }
    
}
extension UIView {
    @objc static var mapy_viewToString: String {
        return NSStringFromClass(self.classForCoder())
    }
    @objc var mapy_viewGetVController: UIViewController? {
        var next = self.next
        while next != nil {
            if let vc = next as? UIViewController {
                return vc
            }
            next = next?.next
        }
        return nil
    }
    @objc func mapy_addTap(target: Any? = nil, action: Selector) {
        self.isUserInteractionEnabled = true
        guard let target = target else {
            let tap = UITapGestureRecognizer.init(target: self, action: action)
            addGestureRecognizer(tap)
            return
        }
        let tap = UITapGestureRecognizer.init(target: target, action: action)
        addGestureRecognizer(tap)
    }
}

extension NSInteger {
    func mapy_formatTime() -> String {
        let hours = self / 3600
        let minutes = (self % 3600) / 60
        let remainingSeconds = self % 60
        
        let formattedTime = String(format: "%02d:%02d:%02d", hours, minutes, remainingSeconds)
        return formattedTime
    }
}
